import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
    ...HttpUtils.func,
    data: {
        pageDetail: 'articleDetail',
        player: 'playerHide',
        pageIndex: 1,
        pageItems: 15,
        totalPages: 0,
        newsItems: []
    },
    onLoad() {
        this.loadNewsList();
    },
    //数据渲染
    loadNewsList: function() {
        var newUrl = getApp().globalData.dangjianUrl + "/party/partyNewsList";
        var params = {};
        params["pageIndex"] = this.data.pageIndex + "";
        params["pageItems"] = this.data.pageItems + "";
        params["name"] = "";
        params["newsType"] = "4";
        params = JSON.stringify(params);

        wx.showLoading({
            title: '玩命加载中',
        });
        this.httpGet(newUrl, params, "支部新闻列表加载失败").then(
            (respData) => {
                var showNewsItems = true;
                var result = respData.data.result.code;
                if (1 == result) {
                    var newsItems = respData.data.newsItems;
                    var totalPages = newsItems.totalPages;
                    newsItems = newsItems.news;
                    if (null != newsItems && undefined != newsItems && newsItems.length > 0) {
                        var recordList = [];
                        for (var index in newsItems) {
                            var obj = newsItems[index];
                            var thumnail = "";
                            if (null != obj.iconList && obj.iconList.length > 0) {
                                thumnail = obj.iconList[0].iconUrl;
                            }
                            obj["thumnail"] = thumnail;
                            obj["publishTime"] = this.formatDate("yyyy-MM-dd", obj.publishTime);
                            obj["sourceFrom"] = "华数";
                            recordList.push(obj);
                        }
                        var oldnewsItems = this.data.newsItems;
                        recordList = oldnewsItems.concat(recordList);
                    } else {
                        showNewsItems = false;
                    }
                    this.setData({
                        "newsItems": recordList,
                        "totalPages": totalPages,
                        "showNewsItems": showNewsItems,
                    });
                    wx.hideLoading();
                } else {
                    this.showPageMsg("alert", respData.data.result.description);
                    wx.hideLoading();
                }
            }
        );
    },
    //跳转到新闻详情页面
    newsDetail: function(e) {
        var newsId = e.currentTarget.dataset.newsid;
        var name = e.currentTarget.dataset.name;
        var detailUrl = "../partyNewsDetail/partyNewsDetail?newsId=" + newsId + '&name=' + name;
        wx.navigateTo({
            url: detailUrl
        });
    },
    //上拉加载
    onReachBottom: function(e) {
        var pageIndex = this.data.pageIndex + 1;
        var totalPages = this.data.totalPages;
        if (totalPages >= pageIndex) {
            this.setData({
                "pageIndex": pageIndex
            });
            this.loadNewsList();
        } else {
            this.showPageMsg("tips", "没有更多数据了");
        }
    },
    //下拉刷新
    onPullDownRefresh: function(e) {
        this.setData({
            pageIndex: 1,
            newsItems: []
        });
        this.loadNewsList();
        wx.stopPullDownRefresh();
    }
});